package org.westboy.service.impl;

import com.google.common.collect.Lists;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.westboy.common.util.DateUtil;
import org.westboy.common.util.HtmlKit;
import org.westboy.model.Order;
import org.westboy.model.OrderPro;
import org.westboy.mybatis.mapper.MallbuilderProductOrderMapper;
import org.westboy.mybatis.mapper.MallbuilderProductOrderProMapper;
import org.westboy.mybatis.model.MallbuilderProductOrder;
import org.westboy.mybatis.model.MallbuilderProductOrderPro;
import org.westboy.service.OrderService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;


@Service
public class OrderServiceImpl implements OrderService{

    @Autowired
    private MallbuilderProductOrderMapper orderMapper;

    @Autowired
    private MallbuilderProductOrderProMapper orderProMapper;

    @Override
    public ObservableList<Order> findTodayOrder() {
        ObservableList<Order> observableList = FXCollections.observableList(Lists.newArrayList());
        orderMapper.findTodayOrder().forEach(o->{
            Order order = new Order();
            order.setId(o.getId());
            order.setOrderId(o.getOrderId());
            order.setConsignee(o.getConsignee());
            order.setCreateTime(o.getCreateTime());
            order.setOrderNote(HtmlKit.getText(o.getOrderNote()));
            order.setPrice(o.getProductPrice());
            order.setDes(HtmlKit.getText(o.getDes()));
            if(o.getStatus().equals(0))
                order.setStatus("订单取消");
            if(o.getStatus().equals(1))
                order.setStatus("未付款");
            if(o.getStatus().equals(2))
                order.setStatus("已付款");
            if(o.getStatus().equals(3))
                order.setStatus("已出库");
            if(o.getStatus().equals(4))
                order.setStatus("交易成功");
            if(o.getStatus().equals(7))
                order.setStatus("货到付款");
            observableList.add(order);
        });
        return  observableList;
    }

    @Override
    public Integer findTodayOrderCount() {
        return orderMapper.findTodayOrderCount();
    }

    @Override
    public ObservableList<OrderPro> findOrderProList(String OrderId) {
        List<MallbuilderProductOrderPro> result;
        List<OrderPro> proList = Lists.newArrayList();
        Integer parentId =orderMapper.findParentIdByOrderId(OrderId);
        List<MallbuilderProductOrder> mallbuilderProductOrders = orderMapper.findByParentId(parentId);
        if(mallbuilderProductOrders!=null&&mallbuilderProductOrders.size()>0) {
            AtomicReference<String> orderIds = new AtomicReference<>();
            mallbuilderProductOrders.forEach(o -> orderIds.set(StringUtils.isBlank(orderIds.get()) ? o.getOrderId().toString() : orderIds.get() + "," + o.getOrderId().toString()));
            result = orderProMapper.findByParentId(orderIds.get());
        }else{
            result = orderProMapper.findByParentId(OrderId);
        }
        result.forEach(o -> {
           OrderPro pro = new OrderPro();
           pro.setName(o.getName());
           pro.setNum(o.getNum());
           pro.setPrice(o.getPrice());
           pro.setPic(o.getPic());
           proList.add(pro);
        });

        return  FXCollections.observableList(proList);
    }

    @Override
    public Integer update(MallbuilderProductOrder productOrder) {
        return orderMapper.updateByPrimaryKeySelective(productOrder);
    }


}
